% @author   Hansimov
% @date     2018.07 ~ 2020.06

% Compile with xelatex
% Make sure to compile this using xelatex + xdvipdfmx.
% \usepackage[hyphens]{url}
% \usepackage[xetex]{graphics}
% \usepackage[xetex]{hyperref}

%% Compile with pdftex - Cannot work currently
% \usepackage[hyphens]{url}
% \usepackage[pdfborder={0 0 0}]{hyperref}
% \hypersetup{CJKbookmarks=true}

\usepackage[scheme = plain]{ctex}
% \usepackage[]{xeCJK}
\xeCJKsetup{CheckSingle = true}

% %% I add these lines to auto-fit the line spacing of Chinese texts and English codes.
% \usepackage{etoolbox}
\usepackage{setspace}

% \usepackage[hang]{footmisc}
% \renewcommand{\footnotelayout}{\setstretch{1.5}}
% \setlength{\footnotemargin}{2mm}

\usepackage{indentfirst}
\setlength{\parindent}{2em}

% Getting verbatim with soft grey background, as in tex.stackexchange
%   https://tex.stackexchange.com/a/141128/135822

% \usepackage{xcolor}

\newcommand{\bohs}{\begin{onehalfspacing}}
\newcommand{\eohs}{\end{onehalfspacing}}

\newcommand{\metazh}[1]{$\langle \text{\emph{#1}} \rangle$}
\newcommand{\metablue}[1]{{\color{blue}$\langle \text{\emph{#1}} \rangle$}}

\linespread{1.25}

\renewcommand{\contentsname}{\centering 目录}

\usepackage{listings}
\lstdefinelanguage{tikz}{
basicstyle={\color{blue} \ttfamily},
breaklines = true,
breakatwhitespace=true,
keepspaces = true,
% showspaces=true,
% escapechar=`,
% mathescape=true,
}

\newcommand{\ltz}[1]{\lstinline[language=tikz]{#1}}
% Some chars must be escaped when using \ltz: \(backslash) |(vertical line) {}(curly braces)

% \usepackage{minted}

% breaklines for in-line verbatim
\RequirePackage{etoolbox}
\makeatletter
\def\@xobeysp{\hspace{0pt}\mbox{}\hspace{0pt}}
\appto\verbatim@font{\hyphenchar\font`-\relax}
\makeatother 

\def\extractenvironement#1#2\@@{%
  \pgfmanualentryheadline{%
    \pgfmanualpdflabel{#1}{}%
    {\ttfamily\char`\\begin\char`\{\declare{#1}\char`\}}#2%
  }%
  \pgfmanualentryheadline{{\ttfamily\ \ }\meta{环境内容}}%
  \pgfmanualentryheadline{{\ttfamily\char`\\end\char`\{\declare{#1}\char`\}}}%
  \index{#1@\protect\texttt{#1} environment}%
  \index{Environments!#1@\protect\texttt{#1}}
}

\def\extractplainenvironement#1#2\@@{%
  \pgfmanualentryheadline{{\ttfamily\declare{\char`\\#1}}#2}%
  \pgfmanualentryheadline{{\ttfamily\ \ }\meta{环境内容}}%
  \pgfmanualentryheadline{{\ttfamily\declare{\char`\\end#1}}}%
  \index{#1@\protect\texttt{#1} environment}%
  \index{Environments!#1@\protect\texttt{#1}}%
}

\def\extractcontextenvironement#1#2\@@{%
  \pgfmanualentryheadline{{\ttfamily\declare{\char`\\start#1}}#2}%
  \pgfmanualentryheadline{{\ttfamily\ \ }\meta{环境内容}}%
  \pgfmanualentryheadline{{\ttfamily\declare{\char`\\stop#1}}}%
  \index{#1@\protect\texttt{#1} environment}%
  \index{Environments!#1@\protect\texttt{#1}}}

\def\example{\par\smallskip\noindent\textit{示例: }}
\def\themeauthor{\par\smallskip\noindent\textit{主题作者: }}

\renewenvironment{filedescription}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{文件~ {\ttfamily\declare{#1}}}%
    \index{#1@\protect\texttt{#1} file}%
    \index{Packages and files!#1@\protect\texttt{#1}}%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\renewenvironment{decoration}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{\textbf{装饰~} {\ttfamily\declare{#1}}}%
    \index{#1@\protect\texttt{#1} decoration}%
    \index{Decorations!#1@\protect\texttt{#1}}
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\renewenvironment{tikzlibrary}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{%
      \pgfmanualpdflabel{#1}{}%
      \textbf{\tikzname\ 库~} \texttt{\declare{#1}}}
    \index{#1@\protect\texttt{#1} library}%
    \index{Libraries!#1@\protect\texttt{#1}}%
    \vskip.25em%
    {{\ttfamily\char`\\usetikzlibrary\char`\{\declare{#1}\char`\}\space\space \char`\%\space\space  \LaTeX\space and plain \TeX}}\\
    {{\ttfamily\char`\\usetikzlibrary[\declare{#1}]\space \char`\%\space\space Con\TeX t}}\\[.5em]
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\renewenvironment{coordinatesystem}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{%
      \pgfmanualpdflabel{#1}{}%
      \textbf{坐标系统~} {\ttfamily\declare{#1}}}%
    \index{#1@\protect\texttt{#1} coordinate system}%
    \index{Coordinate systems!#1@\protect\texttt{#1}}
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\renewenvironment{predefinednode}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{%
      \pgfmanualpdflabel{#1}{}%
      \textbf{预定义节点~} {\ttfamily\declare{#1}}}%
    \index{#1@\protect\texttt{#1} node}%
    \index{Predefined node!#1@\protect\texttt{#1}}
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\renewenvironment{dataformat}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{%
      \pgfmanualpdflabel{#1}{}%
      \textbf{格式~} {\ttfamily\declare{#1}}}%
    \index{#1@\protect\texttt{#1} format}%
    \index{Formats!#1@\protect\texttt{#1}}
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}


\iffalse
% Hyperlinks for text to contents
\RequirePackage{xparse}
% \RequirePackage{hyperref}

\let\oldpart=\part
\RenewDocumentCommand{\part}{s m}{
  \IfBooleanTF{#1}{
    \oldpart*{\protect\hyperlink{part:\thepart}{#2}}
  }{
    \oldpart{\protect\hyperlink{part:\thepart}{#2}}
  }
  \addtocontents{toc}{\protect\hypertarget{part:\thepart}{}}
}

\let\oldsection=\section
\RenewDocumentCommand{\section}{s m}{
  \IfBooleanTF{#1}{
    \oldsection*{\protect\hyperlink{section:\thesection}{#2}}
  }{
    \oldsection{\protect\hyperlink{section:\thesection}{#2}}
  }
  \addtocontents{toc}{\protect\hypertarget{section:\thesection}{}}
}

\let\oldsubsection=\subsection
\RenewDocumentCommand{\subsection}{s m}{
  \IfBooleanTF{#1}{
    \oldsubsection*{\protect\hyperlink{subsection:\thesubsection}{#2}}
  }{
    \oldsubsection{\protect\hyperlink{subsection:\thesubsection}{#2}}
  }
  \addtocontents{toc}{\protect\hypertarget{subsection:\thesubsection}{}}
}

\let\oldsubsubsection=\subsubsection
\RenewDocumentCommand{\subsubsection}{s m}{
  \IfBooleanTF{#1}{
    \oldsubsubsection*{\protect\hyperlink{subsubsection:\thesubsubsection}{#2}}
  }{
    \oldsubsubsection{\protect\hyperlink{subsubsection:\thesubsubsection}{#2}}
  }
  \addtocontents{toc}{\protect\hypertarget{subsubsection:\thesubsubsection}{}}
}
\fi